<?php
class XmailModel extends Model {
	protected	$tableName	=	'x_mail';
	protected	$fields		=	array (0 => 'id',1 => 'uid',2 => 'from_mail',3 => 'to_mail',4 => 'subject',5 => 'content',6 => 'status',7 => 'addtime',8 => 'updatetime',9 => 'level','_autoinc' => true,'_pk' => 'id');

	/*
		邮件列表类：
		1，待发邮件，首先插入列表，状态为1
		2，系统执行邮件发送，每次发送条数可配置，按待发,失败排序
		3，自动发送后，成功状态为2，失败状态为0
		4，清理1天前发送成功的邮件.
	*/

	public function addToList($sendto_email,$subject,$body,$level=1,$senderInfo=false) {
		//发件人
		if(is_array($senderInfo)){
			$from_mail	=	$senderInfo['email'];
		}else{
			$from_mail	=	'';
		}

		//发件时间
		$addtime	=	date('Y-m-d H:i:s');

		//格式化数据
		if(is_array($sendto_email)){
			
			$insert_sql	.=	"INSERT INTO __TABLE__ (`uid`,`from_mail`,`to_mail`,`subject`,`content`,`level`,`addtime`,`status`) VALUES ";

			foreach($sendto_email as $mail){
				$insert_sql	.=	" (".$_SESSION['mid'].",'$from_mail','$mail','$subject','$body','$level','$addtime',1) ,";
			}

			$insert_sql	=	rtrim($insert_sql,',');

			//插入数据列表
			$result	=	$this->execute($insert_sql);

			return $result;
		}else{
			//2010-09-29添加
			$insert_sql	.=	"INSERT INTO __TABLE__ (`uid`,`from_mail`,`to_mail`,`subject`,`content`,`level`,`addtime`,`status`) VALUES ";
			$insert_sql	.=	" (".$_SESSION['mid'].",'$from_mail','$mail','$subject','$body','$level','$addtime',1) ,";
			$insert_sql	=	rtrim($insert_sql,',');
			//插入数据列表
			$result	=	$this->execute($insert_sql);
			return $result;
		}
	}
	
	//设置为成功
	public function setSuccess($ids) {
		$map['id']	=	array('in',$ids);
		return $this->where($map)->setField('status',2);
	}

	//设置为失败
	public function setFail($ids) {
		$map['id']	=	array('in',$ids);
		return $this->where($map)->setField('status',0);
	}
}
?>